﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;
using Telerik.Windows;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;
using Accounting.Library;
using csla4Skeleton.UI.SL.DanhMuc.DM_ChucVu;
using csla4Skeleton.UI.SL.DanhMuc.DM_NganHang;
using csla4Skeleton.UI.SL.DanhMuc.DM_NhomNhaCungCap;
using csla4Skeleton.UI.SL.DanhMuc.DM_LoaiNhaCungCap;
using Converter;
using System.Resources;
using System.Reflection;

namespace csla4Skeleton.UI.SL.DanhMuc.DM_NhaCungCap
{
    public partial class DM_NhaCungCap_ListView : UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            // no parameters
        }

        public string Title
        {
            get { return "NhaCungCap"; }
        }

        #endregion

        #region Load Methods
        public DM_NhaCungCap_ListView()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.DanhMuc.DM_NhaCungCap.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.DM_NhaCungCap_GeneralInformation_GroupBox, controls);
            this.DM_NhaCungCap_GridView.AddHandler(GridViewCellBase.CellDoubleClickEvent, new EventHandler<RadRoutedEventArgs>(OnCellDoubleClick), true);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                System.Windows.Data.CollectionViewSource dm_NhaCungCap_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
                DM_NhaCungCap_ListViewModel dm_NhaCungCap_ListViewModel = new DM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_TatCa);
                dm_NhaCungCap_collectionViewSource.Source = new List<object> { dm_NhaCungCap_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_ChucVu_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ChucVu_ListViewModelSource"];
                DM_ChucVu_ListViewModel dm_ChucVu_ListViewModel = new DM_ChucVu_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_ChucVu_collectionViewSource.Source = new List<object> { dm_ChucVu_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiNhaCungCap_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiNhaCungCap_ListViewModelSource"];
                DM_LoaiNhaCungCap_ListViewModel dm_LoaiNhaCungCap_ListViewModel = new DM_LoaiNhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiNhaCungCap_collectionViewSource.Source = new List<object> { dm_LoaiNhaCungCap_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_NhomNhaCungCap_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhomNhaCungCap_ListViewModelSource"];
                DM_NhomNhaCungCap_ListViewModel dm_NhomNhaCungCap_ListViewModel = new DM_NhomNhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_NhomNhaCungCap_collectionViewSource.Source = new List<object> { dm_NhomNhaCungCap_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_NganHang_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NganHang_ListViewModelSource"];
                DM_NganHang_ListViewModel dm_NganHang_ListViewModel = new DM_NganHang_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_NganHang_collectionViewSource.Source = new List<object> { dm_NganHang_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_NhaCungCap_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ViewModelSource"];
                DM_NhaCungCap_ViewModel dm_NhaCungCap_ViewModel = new DM_NhaCungCap_ViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
                dm_NhaCungCap_ViewSource.Source = new List<object> { dm_NhaCungCap_ViewModel };
                dm_NhaCungCap_ViewModel.Saved += new EventHandler(dm_NhaCungCap_ViewModel_Saved);
              


            }
        }

       
        #endregion

        #region Switch View Methods
        private void SwitchInputForInformationView(string nameOfView)
        {
            if (nameOfView == "GeneralInfoView")
            {
                this.DM_NhaCungCap_GeneralInformation_GroupBox.Visibility = Visibility.Visible;
                this.DM_NhaCungCap_DetailInformation_GroupBox.Visibility = Visibility.Collapsed;
            }
            else if (nameOfView == "DetailInfoView")
            {
                this.DM_NhaCungCap_DetailInformation_GroupBox.Visibility = Visibility.Visible;
                this.DM_NhaCungCap_GeneralInformation_GroupBox.Visibility = Visibility.Collapsed;
            }
        }

        private void DetailInfoView_Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            SwitchInputForInformationView("DetailInfoView");
        }

        private void GeneralInfoView_Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            SwitchInputForInformationView("GeneralInfoView");
        }
        #endregion

        #region  Event Handle

        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, controls);
        }

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {

            ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
        }

        private void DM_NhaCungCap_GeneralInformation_NhomNCCId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_NhomNhaCungCap_ChildWindow dm_NhomNhaCungCap_ChildWindow = new DM_NhomNhaCungCap_ChildWindow();
            dm_NhomNhaCungCap_ChildWindow.Show();
            dm_NhomNhaCungCap_ChildWindow.SaveClicked += new EventHandler(dm_NhomNhaCungCap_ViewModel_Saved);
          
        }

        private void DM_NhaCungCap_GeneralInformation_LoaiNCCId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_LoaiNhaCungCap_ChildWindow dm_LoaiNhaCungCap_ChildWindow = new DM_LoaiNhaCungCap_ChildWindow();
            dm_LoaiNhaCungCap_ChildWindow.Show();
            dm_LoaiNhaCungCap_ChildWindow.SaveClicked += new EventHandler(dm_LoaiNhaCungCap_ViewModel_Saved);

        }

        private void DM_NhaCungCap_GeneralInformation_MoTaiNHId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_NganHang_ChildWindow dm_NganHang_ChildWindow = new DM_NganHang_ChildWindow();
            dm_NganHang_ChildWindow.Show();
            dm_NganHang_ChildWindow.SaveClicked += new EventHandler(dm_NganHang_ViewModel_Saved);

        }
        
        private void DM_NhaCungCap_DetailInformation_ChucVuId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_ChucVu_ChildWindow dm_ChucVu_ChildWindow = new DM_ChucVu_ChildWindow();
            dm_ChucVu_ChildWindow.Show();
            dm_ChucVu_ChildWindow.SaveClicked += new EventHandler(dm_ChucVu_ViewModel_Saved);

        }
       

        #endregion
        #region View Model Related Methods
        void dm_NhaCungCap_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_NhaCungCap_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
            DM_NhaCungCap_ListViewModel vm = ((List<object>)dm_NhaCungCap_ListViewSource.Source)[0] as DM_NhaCungCap_ListViewModel;
            vm.GetDM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_TatCa);
        }


        void dm_LoaiNhaCungCap_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_LoaiNhaCungCap_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiNhaCungCap_ListViewModelSource"];
            DM_LoaiNhaCungCap_ListViewModel vm = ((List<object>)dm_LoaiNhaCungCap_ListViewSource.Source)[0] as DM_LoaiNhaCungCap_ListViewModel;
            vm.GetDM_LoaiNhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_ChucVu_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_ChucVu_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ChucVu_ListViewModelSource"];
            DM_ChucVu_ListViewModel vm = ((List<object>)dm_ChucVu_ListViewSource.Source)[0] as DM_ChucVu_ListViewModel;
            vm.GetDM_ChucVu_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_NhomNhaCungCap_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_NhomNhaCungCap_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhomNhaCungCap_ListViewModelSource"];
            DM_NhomNhaCungCap_ListViewModel vm = ((List<object>)dm_NhomNhaCungCap_ListViewSource.Source)[0] as DM_NhomNhaCungCap_ListViewModel;
            vm.GetDM_NhomNhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        void dm_NganHang_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_NganHang_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NganHang_ListViewModelSource"];
            DM_NganHang_ListViewModel vm = ((List<object>)dm_NganHang_ListViewSource.Source)[0] as DM_NganHang_ListViewModel;
            vm.GetDM_NganHang_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        private void OnCellDoubleClick(object sender, RadRoutedEventArgs args)
        {
            if (this.DM_NhaCungCap_GridView.SelectedItem != null)
            {
                System.Windows.Data.CollectionViewSource dm_NhaCungCap_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ViewModelSource"];
                DM_NhaCungCap_ViewModel vm = ((List<object>)dm_NhaCungCap_ViewSource.Source)[0] as DM_NhaCungCap_ViewModel;
                int parameter = ((DM_NhaCungCapInfo)this.DM_NhaCungCap_GridView.SelectedItem).Khoa;
                Csla.Xaml.ExecuteEventArgs eventObject = new Csla.Xaml.ExecuteEventArgs();
                eventObject.MethodParameter = parameter;
                vm.GetDM_NhaCungCap(this, eventObject);
            }
        }
        #endregion

    }
}
